package org.csu.mypetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.ProductDesc;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.persistence.impl.JsonUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class ProductDescListJson extends HttpServlet {
    CatalogService catalogService = new CatalogService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = null;
        try {
            response.setContentType("text/json");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            pw = response.getWriter();

            ArrayList<String> productDesclist = catalogService.getProductDescList();
            ProductDesc productDesc = new ProductDesc();
            productDesc.setParrot(productDesclist.get(0));
            productDesc.setFish(productDesclist.get(1));
            productDesc.setDog(productDesclist.get(2));
            productDesc.setReptiles(productDesclist.get(3));
            productDesc.setCat(productDesclist.get(4));
            String jsonStr = JSON.toJSONString(productDesc);

            pw.print(jsonStr);
            pw.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (pw != null)
                pw.close();
        }

    }
}